package ${groupId}.${projectName}.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
import org.jasypt.encryption.pbe.config.EnvironmentStringPBEConfig;

/**
 * @description: 数据库密码加密工具类
 * @author zhanWen
 * @date 2019/12/26 16:13
 */
public class EncryptUtils {
    private static final Logger LOG = LoggerFactory.getLogger(EncryptUtils.class);

    /**jasypt 加密算法
     * Pwd.
     */
    public static void enpwd(){
        //加密工具
        StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
        //加密配置
        EnvironmentStringPBEConfig config = new EnvironmentStringPBEConfig();
        config.setAlgorithm("PBEWithMD5AndDES");
        //自己在用的时候更改此密码
        config.setPassword("3F4ZVILUpFXkJ4EF");
        //应用配置
        encryptor.setConfig(config);
        String userText="TEST",
                passwordText="test",
                urlText = "jdbc:oracle:thin:@127.0.0.1:1521:orcl";
        //加密
        String cipherUserText=encryptor.encrypt(userText),
                cipherPasswordText=encryptor.encrypt(passwordText),
                cipherurlText=encryptor.encrypt(urlText);
        LOG.info("账号：{}密码：{}url:{}",cipherUserText,cipherPasswordText,cipherurlText);
    }

    /**jasypt 解密算法
     * De pwd.
     */
    public static void dePwd(){

        //加密工具
        StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
        //加密配置
        EnvironmentStringPBEConfig config = new EnvironmentStringPBEConfig();
        config.setAlgorithm("PBEWithMD5AndDES");
        //自己在用的时候更改此密码
        config.setPassword("123456");
        //应用配置
        encryptor.setConfig(config);
        String ciphertext="PovZgl9pg6IXlalIyavYG6HQBq4NyM96";
        //解密
        String plaintext=encryptor.decrypt(ciphertext);
        LOG.info("{}:{}",ciphertext,plaintext);
    }
    public static void main(String[] args){
        EncryptUtils.enpwd();
    }
}
